package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.service.SetmealService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
@RequestMapping("/report")
public class ReportController {

    @Reference
    MemberService memberService;

    @Reference
    SetmealService setmealService;

    @Reference
    ReportService reportService;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        try {
            Map<String,Object> resultMap = memberService.getMemberReport();
            return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    /*
            memberSex: ['男','女']
            memberSexCount: [
                {value:234, name:'男'},
                {value:1548, name:'女'}
                ]
        * */
    @RequestMapping("/getMemberReportBySex")
    public Result getMemberReportBySex(){
        try {
            List<Map<String,Object>> memberSexCount = memberService.getMemberReportBySex();
            List<Object> memberSex = new ArrayList<>();
            //抽取map集合中name属性值
            for (Map<String, Object> map : memberSexCount) {
                Object sex = map.get("name");
                memberSex.add(sex);
            }
            //创建返回数据map
            Map<String,Object> map = new HashMap<>();
            map.put("memberSex",memberSex );
            map.put("memberSexCount", memberSexCount);
            return new Result(true,MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_SETMEAL_COUNT_REPORT_FAIL);
        }

    }

    /*
          memberAgeRange: ['0-18岁','18-30岁','30-45岁','45岁以上']
          memberAgeRangeCount: [
                                    {value:335, name:'0-18岁'},
                                    {value:310, name:'18-30岁'},
                                    {value:234, name:'30-45岁'},
                                    {value:135, name:'45岁以上'}
                               ]
          */
    @RequestMapping("/getMemberReportByAgeRange")
    public Result getMemberReportByAgeRange(){
        try {
            List<Map<String,Object>> memberAgeRangeCount = memberService.getMemberReportByAgeRange();
            List<Object> memberAgeRange = new ArrayList<>();
            //抽取map集合中name属性值
            for (Map<String, Object> map : memberAgeRangeCount) {
                Object ageRange = map.get("name");
                memberAgeRange.add(ageRange);
            }
            //创建返回数据map
            Map<String,Object> map = new HashMap<>();
            map.put("memberAgeRange", memberAgeRange);
            map.put("memberAgeRangeCount", memberAgeRangeCount);
            return new Result(true,MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_SETMEAL_COUNT_REPORT_FAIL);
        }

    }

    /**
     * {
             setmealNames:['直接访问','邮件营销','联盟广告','视频广告','搜索引擎']
             setmealCount:[
                 {value:335, name:'直接访问'},
                 {value:310, name:'邮件营销'},
                 {value:234, name:'联盟广告'},
                 {value:135, name:'视频广告'},
                 {value:1548, name:'搜索引擎'}
                 ]
         }

         Map
         setmealNames： 集合， 元素是字符串
         setmealCount： 集合， 元素是map， 两个key :value, name
     * @return
     */
    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
        try {
            List<Map<String,Object>> setmealCount = setmealService.getSetmealReport();
            List<Object> setmealNames = new ArrayList<>();
            //抽取map集合中name属性值
            for (Map<String, Object> map : setmealCount) {
                Object name = map.get("name");
                setmealNames.add(name);
            }
            //创建返回数据map
            Map<String,Object> map = new HashMap<>();
            map.put("setmealNames",setmealNames );
            map.put("setmealCount", setmealCount);
            return new Result(true,MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_SETMEAL_COUNT_REPORT_FAIL);
        }


    }

    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        try {
            Map<String,Object >  map = reportService.getBusinessReportData();
            return new Result(true,MessageConst.GET_BUSINESS_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_BUSINESS_REPORT_FAIL);
        }
    }

    @RequestMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletResponse response){
        try {
            Map<String,Object >  map = reportService.getBusinessReportData();
            //获取统计数据
            String reportDate = String.valueOf(map.get("reportDate"));
            String todayNewMember = String.valueOf(map.get("todayNewMember"));
            String totalMember = String.valueOf(map.get("totalMember"));
            String thisWeekNewMember = String.valueOf(map.get("thisWeekNewMember"));
            String thisMonthNewMember = String.valueOf(map.get("thisMonthNewMember"));
            String todayOrderNumber = String.valueOf(map.get("todayOrderNumber"));
            String todayVisitsNumber = String.valueOf(map.get("todayVisitsNumber"));
            String thisWeekOrderNumber = String.valueOf(map.get("thisWeekOrderNumber"));
            String thisWeekVisitsNumber = String.valueOf(map.get("thisWeekVisitsNumber"));
            String thisMonthOrderNumber = String.valueOf(map.get("thisMonthOrderNumber"));
            String thisMonthVisitsNumber = String.valueOf(map.get("thisMonthVisitsNumber"));
            List<Map<String,Object>>  hotSetmeal = (List<Map<String, Object>>) map.get("hotSetmeal");


            //获取模本的流对象
            InputStream inputStream = ReportController.class.getResourceAsStream("/template/report_template.xlsx");
            //创建工作薄对象
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            //获取工作表对象
            XSSFSheet sheet =  workbook.getSheetAt(0);

            //设置统计日期
            //获取行
            XSSFRow row = sheet.getRow(2);
            //获取单元格
            XSSFCell cell = row.getCell(5);
            //设置单元格的数据
            cell.setCellValue(reportDate);

            //设置今日新增会员数
            //获取行
            row = sheet.getRow(4);
            //获取单元格
            cell = row.getCell(5);
            //设置单元格的数据
            cell.setCellValue(todayNewMember);

            //设置总会员数
            //获取行
            row = sheet.getRow(4);
            //获取单元格
            cell = row.getCell(7);
            //设置单元格的数据
            cell.setCellValue(totalMember);




            //设置本周新增会员数
            //获取行
            row = sheet.getRow(5);
            //获取单元格
            cell = row.getCell(5);
            //设置单元格的数据
            cell.setCellValue(thisWeekNewMember);


            //设置本月新增会员数
            //获取行
            row = sheet.getRow(5);
            //获取单元格
            cell = row.getCell(7);
            //设置单元格的数据
            cell.setCellValue(thisMonthNewMember);



            //设置今日预约数
            //获取行
            row = sheet.getRow(7);
            //获取单元格
            cell = row.getCell(5);
            //设置单元格的数据
            cell.setCellValue(todayOrderNumber);

            //设置今日新增会员数
            //获取行
            row = sheet.getRow(7);
            //获取单元格
            cell = row.getCell(7);
            //设置单元格的数据
            cell.setCellValue(todayVisitsNumber);


            //设置本周预约数
            //获取行
            row = sheet.getRow(8);
            //获取单元格
            cell = row.getCell(5);
            //设置单元格的数据
            cell.setCellValue(thisWeekOrderNumber);

            //设置 本周到诊数
            //获取行
            row = sheet.getRow(8);
            //获取单元格
            cell = row.getCell(7);
            //设置单元格的数据
            cell.setCellValue(thisWeekVisitsNumber);


            //设置本月预约数
            //获取行
            row = sheet.getRow(9);
            //获取单元格
            cell = row.getCell(5);
            //设置单元格的数据
            cell.setCellValue(thisMonthOrderNumber);


            //设置本月到诊数
            //获取行
            row = sheet.getRow(9);
            //获取单元格
            cell = row.getCell(7);
            //设置单元格的数据
            cell.setCellValue(thisMonthVisitsNumber);

            int rownum = 12;

            //设置热门套餐
            for (Map<String, Object> setmeal : hotSetmeal) {
                //获取行
                row = sheet.getRow(rownum);
                //获取单元格
                cell = row.getCell(4);
                cell.setCellValue(String.valueOf(setmeal.get("name")));
                cell = row.getCell(5);
                cell.setCellValue(String.valueOf(setmeal.get("setmeal_count")));
                cell = row.getCell(6);
                cell.setCellValue(String.valueOf(setmeal.get("proportion")));

                rownum  ++;
            }

            // 通过输出流进行文件下载
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition","attachment;filename="+reportDate+"_report.xlsx");
            workbook.write(out);
            out.flush();
            out.close();
            workbook.close();




        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/getSpeciallyMemberReport")
    public Result getSpeciallyMemberReport(@RequestBody Map<String,Date> map){

        try {
            Map<String,Object> resultMap = memberService.getSpeciallyMemberReport(map);
            return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }
}
